﻿using System;
using System.Collections.Generic;
using System.Linq;
using Way.EAP.DataAccess.Entity;
using Way.EAP.DataAccess.Regulation;
using InterfaceService.DataAccess;
using System.Configuration;
using System.Data;
using Way.Bus.WMS.Controllers;
using Way.Contract.MVC;
using Way.Bus.WMS.Model;
using Way.Bus.Model;
using Way.Bus.WMS.SF.Model;
using Way.EAP.DataAccess.Data;
using Way.Contract.Data.ControllerData;
using Way.Contract.Data;

namespace Way.Bus.WMS.SF.Controllers
{
    public class SFSellOrderController : StockOutOrderController
    {
        /// <summary>
        /// 描述：圣沣销售出库控制器
        /// 作者：路聪
        /// 创建日期:2016-08-18
        /// </summary>

        public SFSellOrderController() : base(new SFStockOutOrder())
        {
        }

        #region 初始化数据方法

        #region 基础设置
        //显示在菜单
        protected override string OnControllerName()
        {
            return "销售出库";
        }
        //菜单中url路径
        protected override string OnGetPath()
        {
            return "Views/SF/SellStockOutOrder.htm";
        }

        //说明
        protected override string OnGetControllerDescription()
        {
            return "销售出库管理";
        }

        /// <summary>
        /// 重写下推，隐藏了基类中的下推
        /// </summary>
        public override bool IsPushDown
        {
            get
            {
                return true;
            }
        }
        #endregion

        /// <summary>
        /// 初始化显示与否
        /// </summary>
        /// <param name="data"></param>
        /// <param name="item"></param>
        protected override void OnInitViewChildItem(Contract.Data.ModelData data, Contract.Data.ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (data.name.EndsWith("StockOutOrder"))
            {
                //根据销售人员带出所在部门
                if (item.field.ToLower() == "workerid")
                {
                    if (item.foreign != null)
                    {
                        item.foreign.rowdisplay.Add("OrganizationID".ToUpper(), "BUMEN");
                    }
                }
                data.title = "销售出库";
                switch (item.field.ToLower())
                {
                    case "code":
                        item.visible = true;
                        item.index = 1;
                        break;
                    case "remark":
                        item.visible = true;
                        item.index = 2;
                        break;
                    case "clientno":
                        item.visible = true;
                        item.title = "客户名称";
                        item.index = 3;
                        break;
                    case "towhno":
                        item.visible = true;
                        item.title = "出库仓库";
                        item.index = 4;
                        item.required = true;
                        break;
                    case "workerid":
                        item.visible = true;
                        item.title = "销售人员";
                        item.index = 5;
                        item.required = true;
                        break;
                    case "inouttime":
                        item.visible = true;
                        item.disabled = false;
                        item.required = true;
                        item.index = 6;
                        break;
                    case "bumen":
                        item.visible = true;
                        item.index = 7;
                        break;
                    case "fregionid":
                        item.visible = true;
                        item.index = 8;
                        item.required = true;
                        break;
                    case "fptype":
                        item.visible = true;
                        item.index = 8;
                        break;
                    case "createdby":
                    case "creationdate":
                    case "issubmited":
                    case "submitedby":
                    case "submiteddate":
                    case "isreviewed":
                    case "reviewedby":
                    case "revieweddate":
                        item.visible = true;
                        break;
                    default:
                        item.visible = false;
                        break;
                }
            }
            if (data.name.EndsWith("StockOutOrderMaterials"))
            {
                //根据产品带出编码和规格
                if (item.field.ToLower() == "materialcode")
                {
                    if (item.foreign != null)
                    {
                        item.foreign.rowdisplay.Add("CODE", "MCODE");
                        item.foreign.rowdisplay.Add("FMODEL", "FMODEL");
                    }
                }
                switch (item.field.ToLower())
                {
                    case "materialcode":
                        item.index = 1;
                        item.visible = true;
                        break;
                    case "mcode":
                        item.visible = true;
                        item.disabled = true;
                        item.index = 2;
                        break;
                    case "fmodel":
                        item.visible = true;
                        item.disabled = true;
                        item.index = 3;
                        break;
                    case "num":
                        item.visible = true;
                        item.index = 4;
                        item.required = true;
                        break;
                    case "stay1":
                        item.visible = true;
                        item.disabled = true;
                        item.title = "库存数量";
                        item.index=5;
                        break;
                    case "price":
                        item.index = 6;
                        item.visible = true;
                        break;
                    case "remark":
                        item.visible = true;
                        break;
                    case "depotwbs":
                        item.required = false;
                        item.visible = false;
                        break;
                    default:
                        item.visible = false;
                        break;
                }
            }
            if (data.name.EndsWith("StockOutNoticeMaterials"))
                data.visible = false;
        }
        
        #endregion

        #region 重写方法
        public override bool IsReviewedState
        {
            get
            {
                return true ;
            }
        }

        protected override void OnForeignLoading(IModel model, loadItem item)
        {
            ///选择仓库
            if (this.fdata.filedname == "TOWHNO")
            {
                SQLWhere tempSql = new SQLWhere();
                tempSql.ColumnName = "Acc_WMS_WareHouse.WHTYPE";
                tempSql.Relation = "and";
                tempSql.Type = "int";
                tempSql.Value = "0";
                List<SQLWhere> tempList = new List<SQLWhere>();
                tempList.Add(tempSql);
                item.whereList = tempList.ToArray();
            }
            base.OnForeignLoading(model, item);
        }
        public override void MergerOrderMaterials(StockOutOrder order)
        {
        }
        protected override void ValidateStockNum(StockOutOrderMaterials item, int orderid, int stockid)
        {
            if(string.IsNullOrEmpty(item.DEPOTWBS))
                item.DEPOTWBS = "0";
        }
        protected override int StockType()
        {
            return 0;
        }

        public override void OutWarehouse(Way.Bus.Model.BasicInfo info)
        {
            //base.OutWarehouse(info);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="info"></param>
        protected override void OnReviewedData(BasicInfo info)
        {
           // UpdateSourceOrder(info);
            SFStockOutOrder ord = info as SFStockOutOrder;
            string Dwh = ord.GetForeignObject<BusinessCustomer>(this.model.GetDataAction()).Code;
            if (Dwh.Length>4)
                throw new Exception("异常：所选择客户的编码长度大于4，请修改客户编码！");
            base.OnReviewedData(info);
            UpdateOldDB(ord);
        }

        private void UpdateOldDB(SFStockOutOrder info)
        {
            EntityList<SFStockOutOrder> listSFoutorder = new EntityList<SFStockOutOrder>(this.model.GetDataAction());
            listSFoutorder.GetData(" ID=" + info.ID);
            SFStockOutOrder ord = listSFoutorder[0];


            ord.Materials.DataAction = this.model.GetDataAction();
            ord.Materials.GetData();
            string strSql = "";
            string strCon = "";
            string Dwh = ord.GetForeignObject<BusinessCustomer>(this.model.GetDataAction()).ID.ToString().PadLeft(4, '0');
            string Dwmc = ord.GetForeignObject<BusinessCustomer>(this.model.GetDataAction()).CUSTOMERNAME;

            DateTime dt = ord.INOUTTIME;
            int dtYear = dt.Year;
            int dtMonth = dt.Month;
            int dtDay = dt.Day;
            string Rq = dtYear + "," + dtMonth + "," + dtDay;
            string Xh;
            string Bm;
            string Pm;
            string Gg;

            string Ywyh = "";
            string Ywy = "";
            string Qyh = "";
            string Qymc = "";
            string TempQyh = "";
            string TempQymc = "";
            string Fhr = "";
            string Iksl = "";
            string Je = "";
            Xh = "";
            string Ikdh = "";
            string Dj = "";
            if (ord.WORKERID.ToString() != "0")
            {
                EntityList<OfficeWorker> listrn = new EntityList<OfficeWorker>(this.model.GetDataAction());
                listrn.GetData(" ID=" + ord.WORKERID);
                Ywyh = listrn[0].ID.ToString().PadLeft(3, '0');
                Ywy = listrn[0].WorkName;
            }
            if (ord.FREGIONID.ToString() != "0")
            {
                EntityList<Region> listrn = new EntityList<Region>(this.model.GetDataAction());
                listrn.GetData(" ID=" + ord.FREGIONID);
                TempQyh = listrn[0].ID.ToString().PadLeft(3, '0');
                TempQymc = listrn[0].RegionName;
            }
            Fhr = Rq;
            Pm = "鸭产品";
            Ikdh = ord.Code;
            Xh = Ikdh;
            string strOldSql = "";
            DataTable dtOldIpkc = null;
            strCon = ConfigurationManager.AppSettings["FOXXG"].ToString() + "XUSJK.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            for (int i = 0; i < ord.Materials.Count; i++)
            {
                Bm = ord.Materials[i].GetForeignObject<Materials>(this.model.GetDataAction()).Code;
                Gg = ord.Materials[i].GetForeignObject<Materials>(this.model.GetDataAction()).FNAME;
                Iksl = ord.Materials[i].NUM.ToString();
                Dj = ord.Materials[i].PRICE.ToString();
                Je = (ord.Materials[i].PRICE * Convert.ToDouble(Iksl)).ToString();
                strOldSql = "SELECT * from Xusjk where Bm='" + Bm.Substring(3, 6) + "' and Xh='" + Xh + "'";
                dtOldIpkc = BusFoxProHelper.ExecuteDataTable(strCon, CommandType.Text, strOldSql);
                if (ord.Materials[i].Remark == "鲜品")
                {
                    Qyh = this.model.GetDataAction().GetValue("select ID from Acc_Bus_Region where RegionName='鲜品'").ToString().PadLeft(3, '0');
                    Qymc = "鲜品";
                }
                else
                {
                    Qyh = TempQyh;
                    Qymc = TempQymc;
                }
                if (dtOldIpkc.Rows.Count > 0)
                {
                    strSql = "UPDATE Xusjk set Sl=" + Iksl + ",Dj=" + Dj + ",Je=" + Je + ",Dwh='" + Dwh + "', Dwmc='" + Dwmc + "',Hkje=" + Je + ",Ywyh='" + Ywyh + "',Ywy='" + Ywy + "',Qyh='" + Qyh + "',Qymc='" + Qymc + "' where Bm='" + Bm.Substring(3, 6) + "' and Xh='" + Xh + "'";
                }
                else
                {
                    strSql = "insert into Xusjk(Xh,Bm,Ywyh,Ywy,Qyh,Qymc,Fhr,Dwh,Dwmc,Pm,Gg,Sl,Dj,Je,Fph,Til,Pvh,Mbvl,Xuti,Xufy,Jsfu,Shr,Hkr,Hkje,Qkje,Hhqk,Nbjg,Djie,Jgie,Bzvu,Jxrq,Xdju,Lxvi,Lxbl,Thyy,Uirq,Qyfz,Ixb1,Ixb2,Ixb3)  ";
                    strSql = strSql + "values('" + Xh + "','" + Bm.Substring(3, 6) + "','" + Ywyh + "','" + Ywy + "','" + Qyh + "','" + Qymc + "',date(" + Fhr + "),'" + Dwh + "','" + Dwmc + "','" + Pm + "','" + Gg + "'," + Iksl + "," + Dj + "," + Je + ",'" + Xh + "',0,'',0,0,0,'','',date(" + Fhr + ")," + Je + ",0,0,0,0,0,'',date(" + Fhr + "),0,0,0,'',date(" + Fhr + "),'',0,0,'')";
                }
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            }
        }

        /// <summary>
        /// 修改生产计划单据状态
        /// </summary>
        /// <param name="info"></param>
        private void UpdateSourceOrder(BasicInfo info)
        {

            StockOutOrder ord = info as StockOutOrder;
            ord.Materials.DataAction = this.model.GetDataAction();
            ord.Materials.GetData();
            for (int i = 0; i < ord.Materials.Count; i++)
            {
                churukubiao("1", ord.ID.ToString(), ord.Materials[i].MATERIALCODE);
            }

            ///得到销售出库的上游单据（生产计划）
            EntityList<StockInNotice> sinList = new EntityList<StockInNotice>(this.model.GetDataAction());
            sinList.GetData("Code='" + ord.SourceCode + "'");
            sinList[0].Materials.GetData();


            ///得到所有本生产计划下的所有的销售出库单据及明细
            EntityList<StockOutOrder> soList = new EntityList<StockOutOrder>(this.model.GetDataAction());
            soList.GetData("SourceCode='" + sinList[0].Code + "' and StockType='1' and isreviewed = 1");


            EntityList<StockOutOrderMaterials> sm = new EntityList<StockOutOrderMaterials>(this.model.GetDataAction());
            //处理销售单据明细，合并数据
            for (int i = 0; i < soList.Count; i++)
            {
                soList[i].Materials.GetData();
                foreach (StockOutOrderMaterials item in soList[i].Materials)
                {
                    sm.Add(item);
                }
            }
            
            var linqList = LinqHelp.MergerStockOutOrderMaterials(sm);
            var sourceList = sinList[0].Materials;
            ///得到两个List
            //第一种情况，目标单据明细行count大于源单行数量。
            if (linqList.Count > sourceList.Count)
            {
                throw new Exception("异常：销售出库明细与生产计划不符");
            }
            List<Int32> list = new List<int>();
            for (int i = 0; i < sourceList.Count; i++)
            {
                for (int j = i; j < linqList.Count; j++)
                {
                    ///如果两个结果集数量都相同，相当于已经是最后一个出库单，接下来修改原单据状态
                    if (linqList[j].MATERIALCODE == sourceList[i].MATERIALCODE && linqList[j].NUM == sourceList[i].NUM)
                    {
                        list.Add(j);
                        if (list.Count == sourceList.Count)
                        {
                            sinList[0].IsDisable = true;
                        }
                    }
                }
            }
        }

        
        /// <summary>
        /// 出入库流水表---已经更改为修改出库数量，出入库流水表已经删除
        /// </summary>
        /// <param name="portno"></param>
        /// <param name="ordernoid"></param>
        /// <param name="mcNo"></param>
        public void churukubiao(string portno, string ordernoid, string mcNo)
        {
            IDataAction action = this.model.GetDataAction();
            string strSql = "select aaa.*,bbb.Code,bbb.FNAME,ccc.TOWHNO,ccc.Code as outNO,ddd.WBSCODE  from Acc_WMS_StockOutOrderMaterials aaa inner join Acc_WMS_StockOutOrder aws on aws.ID = aaa.parentID ";
            strSql = strSql + "left join Acc_Bus_BusinessCommodity bbb on aaa.MATERIALCODE=bbb.ID left join Acc_WMS_StockOutOrder ccc on ";
            strSql = strSql + "aaa.PARENTID=ccc.ID left join Acc_WMS_WareHouse ddd on aaa.DEPOTWBS=ddd.ID where aaa.PARENTID='" + ordernoid + "' and bbb.ID = '" + mcNo + "'";
            DataTable dt = action.GetDataTable(strSql);
            string materialcode;
            string fname;
            string houseware;
            string num;
            strSql = "";
            if (dt.Rows.Count > 0)
            {
                ///循环出库明细行的count
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    materialcode = dr["Code"].ToString();
                    fname = dr["FNAME"].ToString();
                    houseware = dr["TOWHNO"].ToString();
                    num = dr["NUM"].ToString();
                    ////插入到出库流水表
                    if (Convert.ToDecimal(dr["NUM"]) > Convert.ToDecimal(dr["NUMFH"]))
                    {
                        strSql += "update Acc_WMS_StockOutOrderMaterials set numfh = " + Double.Parse(num) + " where PARENTid ='" + ordernoid + "' and materialcode= '" + mcNo + "';";
                        strSql += "update Acc_WMS_StockOutOrder set IsReviewed=1 where ID ='" + ordernoid + "' ";
                    }
                }
                action.Execute(strSql);
            }

        #endregion

        }


        #region 下推数（销售出库--退货入库  -----------&&----------销售出库--发运单）

        /// <summary>
        /// 单据转换
        /// </summary>
        /// <param name="ca"></param>
        /// <param name="actionItem"></param>
        /// <returns></returns>
        protected override EntityBase OnConvertItem(ControllerAssociate ca, EntityBase actionItem)
        {
            //下推前判断
            WMShelp.IsPush(this);
            StockOutOrder cd = actionItem as StockOutOrder;
            //volipush(cd);//是否可以下推
            cd.Materials.DataAction = this.model.GetDataAction();
            cd.Materials.GetData();//获取子集的数据
            EntityBase eb = base.OnConvertItem(ca, actionItem);
            ///下推退货入库
            eb = PushInorder(eb,cd);
            ///下推发运单
            eb = PushOutorder(eb, cd);
            return eb;
        }

        /// <summary>
        /// 入库下推判断
        /// </summary>
        /// <param name="eb"></param>
        /// <param name="cd"></param>
        /// <returns></returns>
        protected EntityBase PushInorder(EntityBase eb, StockOutOrder cd)
        {
            if (eb is StockInOrder)
            {
                StockInOrder ebin = eb as StockInOrder;
                ebin.SourceCode = cd.SourceCode;
                ebin.STATE = 1;
                ebin.STOCKTYPE = 3;
                ebin.IsSubmited = false;
                ebin.IsReviewed = false;
                ebin.SourceID = cd.ID;
                ebin.SourceCode = cd.Code;
                ebin.SourceController = this.ToString();
                ebin.Code = "";
                foreach (StockInOrderMaterials m in ebin.Materials)
                {
                    m.NUM = m.NUM;// -m.GetSourceNum(this.ToString(), m.SourceRowID);
                }
                ebin.Materials.RemoveAll(delegate(StockInOrderMaterials m)
                {
                    return m.NUM <= 0;
                });

                if (ebin.Materials.Count <= 0)
                {
                    throw new Exception("下推完成不能重复下推");
                }
                eb = ebin;
            }
            return eb;
        }


        protected EntityBase PushOutorder(EntityBase eb, StockOutOrder cd)
        {
            if (eb is ShippingOrder)
            {
                ShippingOrder ebsp = eb as ShippingOrder;
                Dictionary<string, EntityForeignKeyAttribute> dic = ((IEntityBase)ebsp).GetForeignKey();
                string name = eb.GetAppendPropertyKey("PARENTNO");
                ebsp.PARENTNO = cd.ID;
                ebsp[name] = ebsp.GetForeignObject<StockOutOrder>(this.model.GetDataAction()).Code;
                ebsp.SourceID = cd.ID;
                ebsp.SourceCode = cd.Code;
                ebsp.SourceController = this.ToString();
                ebsp.IsSubmited = false;
                ebsp.IsReviewed = false;
                ebsp.STOCKTYPE = 1;
                foreach (ShippingOrderMaterials m in ebsp.Materials)
                {
                    m.SHIPPINGNUM = (Convert.ToDouble(m.SHIPPINGNUM) - m.GetSourceNum(this.ToString(), Convert.ToInt32(m.SourceID),m.MATERIALCODE)).ToString();
                }
                ebsp.Materials.RemoveAll(delegate(ShippingOrderMaterials m)
                {
                    return Convert.ToDouble(m.SHIPPINGNUM) <= 0;
                });

                if (ebsp.Materials.Count <= 0)
                {
                    throw new Exception("下推完成不能重复下推");
                }
                eb = ebsp;
            }
            return eb;
        }
        /// <summary>
        /// 单据转换
        /// </summary>
        /// <returns></returns>
        protected override ControllerAssociate[] DownAssociate()
        {
            List<ControllerAssociate> list = new List<ControllerAssociate>();
            //出库单下推退货单
            ListStockSellinorder(list);
            //OutOrderToShippOrder(list);
            return list.ToArray();
        }


        /// <summary>
        /// 作者：柳强
        /// 出库单下推退货入库单
        /// </summary>
        /// <param name="list"></param>
        private void ListStockSellinorder(List<ControllerAssociate> list)
        {
            //控制器转换器(出库单-入库单)
            ControllerAssociate ca = new ControllerAssociate(this, new StockReturnInOrderController());
            //单据属性映射
            PropertyMap mapORDERID = new PropertyMap();
            mapORDERID.TargerProperty = "SourceCode";
            mapORDERID.SourceProperty = "Code";
            ca.Convert.AddPropertyMap(mapORDERID);

            //单据主表属性映射
            PropertyMap map2 = new PropertyMap();
            map2.TargerProperty = "IsSubmited";//是否提交
            map2.IsValue = true;
            map2.Value = "false";
            ca.Convert.AddPropertyMap(map2);

            //单据主表属性映射
            PropertyMap map3 = new PropertyMap();
            map3.TargerProperty = "IsReviewed";//是否审核
            map3.IsValue = true;
            map3.Value = "false";
            ca.Convert.AddPropertyMap(map3);

            //实体类型转换器（出库单明细明细-入库单明细）
            ConvertAssociate c = new ConvertAssociate();
            c.SourceType = typeof(StockOutOrderMaterials);//下推来源单据子集
            c.TargerType = typeof(StockInOrderMaterials);//下推目标单据子集

            YS(c);

            ca.AddConvert(c);
            list.Add(ca);
        }


        /// <summary>
        /// 出库单-发运单(下推关联)
        /// </summary>
        /// <param name="list"></param>
        private void OutOrderToShippOrder(List<ControllerAssociate> list)
        {
            //控制器转换器(入库通知-入库单)
            ControllerAssociate bl = new ControllerAssociate(this, new ShippingOrderController());
            //单据属性映射
            PropertyMap mapORDERID = new PropertyMap();
            ///单号ID --来源单号
            mapORDERID.TargerProperty = "SourceCode";
            mapORDERID.SourceProperty = "Code";
            bl.Convert.AddPropertyMap(mapORDERID);

            ///出库类型 ---发运类型
            PropertyMap mapMATERIALSID = new PropertyMap();//ID
            mapMATERIALSID.SourceProperty = "STOCKTYPE";
            mapMATERIALSID.TargerProperty = "SHIPPINGTYPE";
            bl.Convert.AddPropertyMap(mapMATERIALSID);

            //实体类型转换器（出库单-发货通知）
            ConvertAssociate NoticeMaterialsToOrderMaterials = new ConvertAssociate();
            NoticeMaterialsToOrderMaterials.SourceType = typeof(StockOutOrderMaterials);//下推来源单据子集
            NoticeMaterialsToOrderMaterials.TargerType = typeof(ShippingOrderMaterials);//下推目标单据子集
            

            PropertyMap map11 = new PropertyMap();//ID
            map11.SourceProperty = "NUM";
            map11.TargerProperty = "SHIPPINGNUM";
            NoticeMaterialsToOrderMaterials.AddPropertyMap(map11);

            PropertyMap map12 = new PropertyMap();//ID
            map12.SourceProperty = "MATERIALCODE";
            map12.TargerProperty = "SHIPPINGMATERIALS";
            NoticeMaterialsToOrderMaterials.AddPropertyMap(map12);
            YSShippingOrder(NoticeMaterialsToOrderMaterials);
            bl.AddConvert(NoticeMaterialsToOrderMaterials);
            list.Add(bl);
        }

        /// <summary>
        /// 映射
        /// </summary>
        /// <param name="c"></param>
        public void YSShippingOrder(ConvertAssociate c)
        {
            PropertyMap mapMATERIALSID = new PropertyMap();//ID
            mapMATERIALSID.SourceProperty = "CODE";
            mapMATERIALSID.TargerProperty = "SourceCode";
            c.AddPropertyMap(mapMATERIALSID);

            PropertyMap map11 = new PropertyMap();//ID
            map11.SourceProperty = "p.ID";
            map11.TargerProperty = "SourceID";
            PropertyMap map12 = new PropertyMap();//ID
            map12.TargerProperty = "SourceController";
            map12.IsValue = true;
            map12.Value = this.ToString();
            PropertyMap map13 = new PropertyMap();//ID
            map13.SourceProperty = "PARENTID";
            map13.TargerProperty = "SourceRowID";

            PropertyMap map14 = new PropertyMap();//ID
            map14.SourceProperty = "p.Code";
            map14.TargerProperty = "SourceCode";

            PropertyMap map15 = new PropertyMap();//ID
            map15.TargerProperty = "SourceName";
            map15.IsValue = true;
            map15.Value = ((IController)this).ControllerName();

            PropertyMap map16 = new PropertyMap();//ID
            map16.TargerProperty = "SourceTable";
            map16.IsValue = true;
            map16.Value = new StockOutOrderMaterials().ToString();

            c.AddPropertyMap(map11);
            c.AddPropertyMap(map12);
            c.AddPropertyMap(map13);
            c.AddPropertyMap(map14);
            c.AddPropertyMap(map15);
            c.AddPropertyMap(map16);
        }

        /// <summary>
        /// 映射
        /// </summary>
        /// <param name="c"></param>
        public void YS(ConvertAssociate c)
        {
            PropertyMap mapMATERIALSID = new PropertyMap();//ID
            mapMATERIALSID.SourceProperty = "CODE";
            mapMATERIALSID.TargerProperty = "SourceCode";
            c.AddPropertyMap(mapMATERIALSID);

            PropertyMap map11 = new PropertyMap();//ID
            map11.SourceProperty = "p.ID";
            map11.TargerProperty = "SourceID";
            PropertyMap map12 = new PropertyMap();//ID
            map12.TargerProperty = "SourceController";
            map12.IsValue = true;
            map12.Value = this.ToString();
            PropertyMap map13 = new PropertyMap();//ID
            map13.SourceProperty = "ID";
            map13.TargerProperty = "SourceRowID";

            PropertyMap map14 = new PropertyMap();//ID
            map14.SourceProperty = "p.Code";
            map14.TargerProperty = "SourceCode";

            PropertyMap map15 = new PropertyMap();//ID
            map15.TargerProperty = "SourceName";
            map15.IsValue = true;
            map15.Value = ((IController)this).ControllerName();

            PropertyMap map16 = new PropertyMap();//ID
            map16.TargerProperty = "SourceTable";
            map16.IsValue = true;
            map16.Value = new StockOutOrderMaterials().ToString();

            c.AddPropertyMap(map11);
            c.AddPropertyMap(map12);
            c.AddPropertyMap(map13);
            c.AddPropertyMap(map14);
            c.AddPropertyMap(map15);
            c.AddPropertyMap(map16);
        }
        #endregion

        [ActionCommand(name = "财务打印", title = "财务打印", index = 10, icon = "icon-search", isalert = true, onclick = "openAutoNext")]
        public void test1()
        {
            Console.WriteLine("aaaa");
        }
        [ActionCommand(name = "普通打印", title = "普通打印", index = 11, icon = "icon-search", isalert = true, onclick = "openAutoTwo")]
        public void test2()
        {
            Console.WriteLine("aaaa");
        }

        /// <summary>
        /// 得到相应产品的库存数量
        /// </summary>
        [WhereParameter]
        public string materialId { get; set; }
        public void GetKCNum()
        {
            string strSql = "select isnull(SUM(NUM),0) NUM from Acc_WMS_InfoMaterials where Code='" + materialId + "'";
            string kcnum = this.model.GetDataAction().GetValue(strSql).ToString();
            throw new Exception(kcnum);
        }
        [WhereParameter]
        public string sellorderid { get; set; }
        public void updateorderstate()
        {
            if (!string.IsNullOrEmpty(sellorderid))
            {
                EntityList<SFStockOutOrder> stoo = new EntityList<SFStockOutOrder>(this.model.GetDataAction());
                stoo.GetData("ID='" + sellorderid + "'");
                UpdateOldDB(stoo[0]);
                sellorderid = "";
            }
        }
        public void printvali()
        {
            if (!string.IsNullOrEmpty(sellorderid))
            {
                string strSqlVali = "select B.IsReviewed from Acc_WMS_InOutOrderList A left join Acc_WMS_OutOrder B ON A.ORDERNO=B.ID  where A.ORDERNO=" + sellorderid;
                DataTable dtvali =this.model.GetDataAction().GetDataTable(strSqlVali);
                sellorderid = "";

                if (dtvali.Rows.Count == 0)
                    throw new Exception("单据未操作，不能打印！");
                else
                {
                    if (dtvali.Rows[0]["IsReviewed"].ToString() != "1" && dtvali.Rows[0]["IsReviewed"].ToString() != "True")
                        throw new Exception("单据为审核，不能打印！");
                    else
                        throw new Exception("Y");
                }
            }
        }


        [WhereParameter]
        public string strExecuteSql { get; set; }
        public string ExecuteSql()
        {
            IDataAction action = this.model.GetDataAction();
            action.Execute(strExecuteSql);
            return "Y";
        }
    }
}
